package com.example.demo.controller;

import com.example.demo.common.AppResult;
import com.example.demo.common.AppVar;
import com.example.demo.common.PasswordUtils;
import com.example.demo.model.UserInfo;
import com.example.demo.service.UserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2023-11-27
 * Time: 9:52
 */
@Api(tags = "用户 接口")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    // 注册功能
    @PostMapping("/reg")
    public AppResult reg(UserInfo userInfo) {
        // 1.校验参数
        if (userInfo == null || !StringUtils.hasLength(userInfo.getUsername().trim())
                || !StringUtils.hasLength(userInfo.getPassword().trim())
                || !StringUtils.hasLength(userInfo.getEmail().trim())) {
            return AppResult.failed("非法传参!");
        }
        // 2.将数据保存到数据库
        // 加盐加密
        userInfo.setPassword(PasswordUtils.encrypt(userInfo.getPassword()));
        userService.reg(userInfo);
        // 3.将数据库执行结果返回给前端页面
        return AppResult.success();
    }

    // 登录功能
    @PostMapping("/login")
    public AppResult login(UserInfo userInfo, HttpServletRequest request) {
        // 1.校验参数
        if (userInfo == null || !StringUtils.hasLength(userInfo.getUsername().trim())
                || !StringUtils.hasLength(userInfo.getPassword().trim())) {
            return AppResult.failed("非法传参!");
        }
        // 2.根据用户名查询用户
        UserInfo user = userService.getUserByName(userInfo.getUsername());
        if (user == null) {
            return AppResult.failed("用户名或密码错误!");
        }
        // 3.进行密码比对
        if (!PasswordUtils.decrypt(userInfo.getPassword(),user.getPassword())) {
            return AppResult.failed("用户名或密码错误!");
        }
//        System.out.println(user.getUsername());
//        System.out.println(userInfo.getUsername());
        // 4.存储 session
        HttpSession session = request.getSession();
//        System.out.println(userInfo);
        session.setAttribute(AppVar.SESSION_USERINFO_KEY,user); // 在这里吃了亏！！！
//        System.out.println(session.getAttribute(AppVar.SESSION_USERINFO_KEY));
        // 5.将结果返回给前端
        return AppResult.success();
    }

    // 注销功能
    @GetMapping("/logout")
    public AppResult logout(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        // 如果存在 session ，则删除 session
        if (session != null &&
                session.getAttribute(AppVar.SESSION_USERINFO_KEY) != null) {
            session.removeAttribute(AppVar.SESSION_USERINFO_KEY);
        }
        return AppResult.success();
    }
}

